<!-- 登录页 -->
<template>
  <div style="height: 100vh;overflow: hidden;background-image: linear-gradient(135deg, #d0f0d0, #70c070);">
    <el-container style="padding: 40px">
      <el-header>
        <el-row>
          <el-col :span="3">
            <img src="imgs/login/logo.png" style="height: 400px;position: relative;right: 130px;bottom: 50px">
          </el-col>
          <el-col :span="1">
            <img src="imgs/login/auto.png" style="height: 80px;position: relative;bottom: 14px">
          </el-col>
          <el-col :span="1">
            <img src="imgs/login/verticalLine.png" style="height: 50px">
          </el-col>
          <el-col :span="19">
            <span style="color: #fff;font-weight: bold;font-size: 30px">学茶商城运营管理平台</span>
          </el-col>
        </el-row>
      </el-header>
      <el-main>
        <el-row>
          <el-col :span="16">
            <img src="imgs/login/bg.png" style="width: 100%;margin-top: 60px">
          </el-col>
          <el-col :span="8">
            <img src="imgs/login/bg-left.png" style="height: 500px">
          </el-col>
        </el-row>

        <!-- 新增定位容器 -->
        <div style="position: absolute; top: 40%; left: 50%; transform: translate(-50%, -50%); z-index: 1; width: 500px;">
          <el-card style="margin-top: 80px;">
            <h3 style="text-align: center;">用户登录</h3>
            <el-form label-position="top">
              <el-form-item label="用户名">
                <el-input placeholder="请输入用户名" v-model="user.username" @keydown.enter="login"></el-input>
              </el-form-item>
              <el-form-item label="密码">
                <el-input placeholder="请输入密码" show-password v-model="user.password" @keydown.enter="login"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" style="width: 100%" @click="login">登录</el-button>
              </el-form-item>
            </el-form>
            <div style="font-size: 14px;color: #7e7e7e;text-align: center;padding: 10px 0">
              (体验账号:tom 密码:123456)
            </div>
          </el-card>
        </div>
      </el-main>



    </el-container>
  </div>
</template>

<script setup>
import {ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";
import router from "@/router";

const user = ref({username:'',password:''});
//定义登录方法
const login = ()=>{
  console.log(user.value);
  /* 前端通过axios给后端发送请求 .then()回调函数与前面是异步操作 */
  axios.post(BASE_URL+'/v1/user/login',user.value)
      .then((response)=>{
        /* .then()回调函数，用于处理后端返回的想响应数据
           response响应对象 response.data后端响应的JsonResult */
        //console.log(response.data);
        if (response.data.code == 2000){
          ElMessage.success('登录成功!');
          //localStorage存储的是字符串类型的键值对 <key,value>
          //console.log(response.data.data); //是JsonResult对象中data属性的值，也就是后端返回的userVO
          //console.log(JSON.stringify(response.data.data)); //将对象转成JSON格式的字符串
          localStorage.setItem('user',JSON.stringify(response.data.data));
          /*
              获取数据的写法：localStorage.user
              通过存时的键名来取值，取出来的是字符串格式
              可以 JSON.parse(localStorage.user)转回JS对象
           */
          router.push('/');
        }else {
          ElMessage.error(response.data.msg);
          user.value={username:'',password:''}
          // localStorage.clear(); //清空数据
        }
      });

}
</script>

<style scoped>

</style>